package org.dromara.system.service.impl.trading.channel;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessException;
import org.dromara.system.config.core.RedisDelayMQSender;
import org.dromara.system.converter.MachineConverters;
import org.dromara.system.domain.ProductLineInformation;
import org.dromara.system.domain.TradingRecords;
import org.dromara.system.domain.bo.kq.DownstreamApiBean;
import org.dromara.system.domain.bo.kq.KQTradingWaterBo;
import org.dromara.system.domain.vo.lsqdb.LSQDTradingVo;
import org.dromara.system.enums.NotificationEnum;
import org.dromara.system.enums.RedisMQConstant;
import org.dromara.system.service.impl.trading.AbstractTradingService;
import org.dromara.system.util.CommonConstant;
import org.dromara.system.util.JsonUtil;
import org.dromara.system.util.kq.DownstreamApiSecurityUtil;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service("kQTradingService")
@RequiredArgsConstructor
public class KQTradingService extends AbstractTradingService<Object, KQTradingWaterBo> {

    @Resource
    private RedisDelayMQSender redisDelayMQSender;

    @Override
    public NotificationEnum getNotificationEnum() {
        return NotificationEnum.KQ;
    }


    @Override
    public void tradingWaterConvert(KQTradingWaterBo reqDTO) {
        TradingRecords tradingRecords = MachineConverters.INSTANCE.kQRecordsToReceiveDealRecords(reqDTO);
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(CommonConstant.DATE_TIME_FORMAT_PATTERN);
            Date date = formatter.parse(reqDTO.getCompleteTime());
            tradingRecords.setTradingTime(date);
        } catch (Exception e) {
            tradingRecords.setTradingTime(DateUtil.date());
        }
        R<TradingRecords> tradingRecordsR = dealUserTrading(tradingRecords);
    }

    @Override
    public Object tradingWaterAssemble(String mapBody, Integer mark) {
        log.info("快钱交易入参=={}", mapBody);
        ProductLineInformation productLineInformation = getBrandProductsLineById(mark);
        BusinessException.isNull(productLineInformation, "关联权限不存在");
        // 对请求报文进行处理
        DownstreamApiBean apiBean = JSONUtil.toBean(mapBody, DownstreamApiBean.class);
        String responseData = "";
        // 获取请求实体
        try {
            responseData = DownstreamApiSecurityUtil.validSignAndGetBody(apiBean, productLineInformation, String.class);
        } catch (Exception e) {
            BusinessException.isTrue(Boolean.TRUE, "快钱交易解密失败！");
        }
        KQTradingWaterBo kqTradingWaterBo = JSONUtil.toBean(responseData, KQTradingWaterBo.class);
        redisDelayMQSender.send(RedisMQConstant.TRADING_QUEUE,
            JsonUtil.toJSONString(kqTradingWaterBo), getNotificationEnum(), Convert.toStr(kqTradingWaterBo.getOrderNo()));
        return new LSQDTradingVo();
    }
}
